<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 3/26/14
 * Time: 11:09 AM
 */

class SC_Controller_Action extends Zend_Controller_Action{
    public function init(){

    }

    protected function loadTemplate($template_path, $fileConfig = 'template.ini',$sectionConfig = 'template'){
        
        //Xoa nhung du cua layout truoc
        $this->view->headTitle()->set('');
        $this->view->headMeta()->getContainer()->exchangeArray(array());
        $this->view->headLink()->getContainer()->exchangeArray(array());
        $this->view->headScript()->getContainer()->exchangeArray(array());
        
        $filename = $template_path . "/" . $fileConfig;
        $section = $sectionConfig;
        $config = new Zend_Config_Ini($filename,$section);
        $config = $config->toArray();
        
        $baseUrl = $this->_request->getBaseUrl();
        $templateUrl = $baseUrl .$config['url'];
        $cssUrl = $templateUrl . $config['dirCss'];
        $jsUrl = $templateUrl . $config['dirJs'];
        $imgUrl = $templateUrl . $config['dirImg'];
        
        //Nap title cho layout
        $this->view->headTitle($config['title']);
        
        //Nap cac the meta vao layout
        if(count($config['metaHttp'])>0){       
            foreach ($config['metaHttp'] as $key => $value){
                $tmp = explode("|",$value);             
                $this->view->headMeta()->appendHttpEquiv($tmp[0],$tmp[1]);
            }
        }
        
        
        //Nap cac tap tin CSS vao layout
        if(count($config['fileCss'])>0){        
            foreach ($config['fileCss'] as $key => $css){
                $this->view->headLink()->appendStylesheet($cssUrl . $css,'screen');
            }
        }
        
        //Nap cac tap tin javascript cho layout
        if(count($config['fileJs'])>0){     
            foreach ($config['fileJs'] as $key => $js){
                $this->view->headScript()->appendFile($jsUrl . $js,'text/javascript');
            }
        }
        
        $this->view->templateUrl = $templateUrl;
        $this->view->cssUrl = $cssUrl;
        $this->view->jsUrl = $jsUrl;
        $this->view->imgUrl = $imgUrl;
        /*echo '<pre>';
        print_r($config);
        echo '</pre>';*/
        $option = array('layoutPath'=> $template_path, 'layout'=> $config['layout']);
        Zend_Layout::startMvc($option);
        
    }
} 